from db_connect import ConnMysqlSlave
from db_connect import ConnMysql
import aiomysql

class Product():

    def __init__(self,product_name,product_price,product_image,product_type_id):

        self.product_name = product_name
        self.product_price = product_price
        self.product_image = product_image
        self.product_type_id = product_type_id

    
    async def result(self):

        if not (self.product_name and self.product_price and self.product_image and self.product_type_id):

            return {
                'status': -1,
                'errorCode': 444,
                'errorMsg': '参数不能为空',
                'resultBody': None,
            }

        with open("static/product_img/{}.jpg".format(self.product_name),"wb") as f:

            f.write(self.product_image.body)

        # 查找一下product表里面是否有要传的参数
        async with ConnMysqlSlave().pool.acquire() as c:

            async with c.cursor(aiomysql.DictCursor) as cur:

                sql = "SELECT * FROM product WHERE product_name=%s "

                info = await cur.execute(sql,(self.product_name, ))

                print(info)

                product_info = await cur.fetchone()

                print(product_info)

        # 往表里面插入参数
        if not product_info:

            async with ConnMysql().pool.acquire() as c:

                async with c.cursor(aiomysql.DictCursor) as cur: 

                    sql = "INSERT INTO product(product_name,product_price,product_image) VALUES (%s,%s,%s)"

                    await cur.execute(sql,(self.product_name, self.product_price, "static/product_img/{}.jpg".format(self.product_name)))

                    await c.commit()

                    # 插入参数之后 再次查询库里的参数
                    sql_select = "SELECT * FROM product WHERE product_name=%s "

                    await cur.execute(sql_select,(self.product_name,))

                    product_info = await cur.fetchone()
        
        # 通过上面的查找 找到对应的id
        product_id = product_info['id']
        print(product_id)
        
        # 查看intermediate_table表里面是否有传入的参数
        async with ConnMysqlSlave().pool.acquire() as c:

            async with c.cursor(aiomysql.DictCursor) as cur:

                sql = "SELECT * FROM intermediate_table WHERE product_id = %s and product_type_id=%s"
                
                await cur.execute(sql,(product_id,self.product_type_id))

                product_info = await cur.fetchone()

                print(product_info)

        # 没有找到的话  往intermediate_table表里面插入对应的product_id 和 product_type_id
        if not product_info:

            async with ConnMysql().pool.acquire() as c:

                async with c.cursor(aiomysql.DictCursor) as cur:

                    sql = "INSERT INTO intermediate_table (product_id,product_type_id) VALUES (%s,%s)"

                    await cur.execute(sql, (product_id, self.product_type_id))

                    await c.commit()

        
        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': 'OK',
        }


